home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 963 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.0 KB

  1. From: np2@doc.ic.ac.uk (Nat Pryce)
  2. Message-ID: <4k06dh$kni@penguin.doc.ic.ac.uk>
  3. X-Original-Date: 4 Apr 1996 09:57:05 GMT
  4. Path: in2.uu.net!bounce-back
  5. Date: 04 Apr 96 23:45:38 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Re: string::npos is unhandy and dangerous
  9. Organization: Dept. of Computing, Imperial College, University of London, UK.
  10. References: <199604020902.LAA06787@bredex.bredex.de>
  11. X-Newsreader: knews 0.9.4
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBFAgUBMWRfReEDnX0m9pzZAQFhaAF8CQUQVZdHd7T4/KX8tsNym+raE0HlW1nX
  14.     x8Bvpmayht/JJZ2kOb6jYYG95htfsbdo
  15.     =Xt2p
  16.  
  17. In article <199604020902.LAA06787@bredex.bredex.de>,
  18. Nico Josuttis <nico@bredex.de> writes:
  19. >
  20. >Following the standard, all find() member functions for string
  21. >return string::size_type and may have the value string::npos,
  22. >which is (size_type)-1.
  23.  
  24.    ... example elided...
  25.  
  26. >    This seems unhandy und thus dangerous as people might
  27. >    try to make the code simpler:
  28. >    For example:
  29. >    int pos
  30. >    pos = s.find('x');
  31. >    if (pos == string::npos)
  32. >    or even
  33. >    if (pos == -1)
  34. >
  35. >    The problem is that the test with operator== fails if
  36. >    sizeof(size_type) != sizeof(int)
  37. >    because unfortunately then
  38. >    (size_type)-1 is not -1
  39. >    This is due to the conversion to unsigned.
  40. >
  41. >    Am I missing something or are these problems known?
  42. >    Why wasn't taken a simpler and safer interface?
  43.  
  44. Obviously these problems are known, because that is why the type 
  45. string::size_type and the constant string::npos were defined. This
  46. is the safe interface, and I don't think it can be any simpler.
  47. In fact I feel it is easier to read than "if( pos == -1 )" and
  48. hides the implementation of the string. Is it really a problem to
  49. type 14 extra characters, expecially when the result is code
  50. which is more readable, easier to maintain, and *works*?
  51. Basically, if you don't write to the interface then you cannot
  52. expect your code to work in all cases. That is the point of an 
  53. interface, after all.
  54.  
  55. >
  56. > 2.) I wonder what "npos" means.
  57. >     Could somebody tell me?
  58. >
  59.  
  60. How about "no position"?
  61.  
  62.  
  63.  
  64. Cheers,
  65.         Nat.
  66. -- 
  67. +-------------------------------------------+--------------------------------+
  68. | Name:   Nat Pryce MEng ACGI            O- | Mail:  Department of Computing,|
  69. | Email:  np2@doc.ic.ac.uk                  |        Imperial College,       |
  70. | Tel:    +44 (0)171 594 8394 (Direct Dial) |        180 Queen's Gate,       |
  71. | Fax:    +44 (0)171 581 8024               |        London SW7 2BZ,         |
  72. | WWW:    http://www-dse.doc.ic.ac.uk/~np2  |        United Kingdom          |
  73. +-------------------------------------------+--------------------------------+
  74. ---
  75. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  76. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  77. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  78. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  79. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  80.